import phoneHtml from 'ngtemplate!html!../../../tpls/validation/phone.html';
import passwordHtml from 'ngtemplate!html!../../../tpls/validation/password.html';

class RegisterController {
    constructor($rootScope, $scope, $state, $cookieStore, $timeout, ngDialog, dialogService, UserService){
        
        var vm = this;

        vm.$rootScope    = $rootScope;
        vm.$state        = $state;
        vm.$cookieStore  = $cookieStore;
        vm.$timeout      = $timeout;
        vm.ngDialog      = ngDialog;
        vm.dialogService = dialogService;
        vm.UserService   = UserService;

        vm.phoneHtml    = phoneHtml;
        vm.passwordHtml = passwordHtml;

        $rootScope.loadingText = true;

        // 密码正则
        var psw_exp = /^[a-zA-Z]+[0-9]+$/;

        // 模态框提示文本
        vm.common = {
            text: '注册成功'
        }

        // 初始化表单数据
        vm.user = {
            username: '',
            password: '',
            code: ''
        }

        // 获取手机验证码
        vm.getCode = function(isValid) {

            var vm = this;

            if (!isValid) return false;

            if (isValid&&$rootScope.loadingText) {

                // 获取手机验证码
                vm.UserService.postVerifyphone({cellphone: vm.user.username})
                .then(data => {

                    // 判断是否注册
                    data.code!=0&&vm.dialogService.error(data.message)

                    console.log(data)
                }, error => console.log('error'))
            }

            return false;
        }

        // 提交表单
        vm.submitForm = function(isValid) {

            var vm = this;
            
            // 验证失败
            if (!isValid) {
                if (!vm.user.username) {
                    $.blink(angular.element(document.getElementsByName('username')), 5, 100)
                } else if (!vm.user.password) {
                    $.blink(angular.element(document.getElementsByName('password')), 5, 100)
                } else if (!vm.user.code) {
                    $.blink(angular.element(document.getElementsByName('code')), 5, 100)
                } 
            }

            // 验证成功
            if (isValid) {

                // 拼接POST数据格式
                var params = {
                    cellphone: vm.user.username,
                    password: vm.user.password,
                    code: vm.user.code, 
                }

                console.log(params)

                // 用户注册
                vm.UserService.postRegister(params)
                .then(data => {

                    // 用户成功注册
                    data.code==0&&vm.dialogService.success(data.message, 'loginindex.login');

                    // 用户名已经存在
                    data.code!=0&&vm.dialogService.error(data.message);

                    console.log(data)
                }, error => console.log('error'))
            }

            return false;
        }
    }

}

RegisterController.$inject = ['$rootScope', '$scope', '$state', '$cookieStore', '$timeout', 'ngDialog', 'dialogService', 'UserService']; 

export default RegisterController;